﻿/*
 * @Autor: PK
 * @Date: 2021/12/25 16:09:SS
 */

using System;
using System.Collections.Generic;
using System.Linq;
using ShkSimulation.core.template;
using ShkSimulation.core.template.agency;
using ShkSimulation.core.util;

namespace ShkSimulation.core.dao.statistic {
	
	public delegate Dictionary<string, List<DataReporter>> DataReporterGenerateDelegate(Network network);

	public class DataReporterGenerator {

		public DataReporterGenerateDelegate DataReporterGenerateAction { get; set; } =
			DefaultDataReporterGenerateAction;

		public DataReporterGenerator(DataReporterGenerateDelegate functions = null) {
			if (functions == null) {
				return;
			}
			DataReporterGenerateAction = null;
			foreach (var @delegate in functions.GetInvocationList()) {
				var func = (DataReporterGenerateDelegate) @delegate;
				DataReporterGenerateAction += func;
			}
		}
		
		public DataReporterGenerator Clone() {
			return new DataReporterGenerator(DataReporterGenerateAction);
		}

		public static Dictionary<string, List<DataReporter>> DefaultDataReporterGenerateAction(Network network) {
			NetworkInputData inputData = network.NetworkInput;
			Dictionary<string, List<DataReporter>> result = new Dictionary<string, List<DataReporter>>();
			if (network is DataReporter) {
				result.Add("network", new List<DataReporter>(){ (DataReporter)network });
			}
			List<DataReporter> reporters = new List<DataReporter>();
			foreach (var (_, value) in inputData.AllAgencies) {
				reporters.Add(value);
			}
			result.Add("AllAgencies", reporters);
			
			if (inputData.ClassifiedAgencies == null) {
				return result;
			}
			foreach (var (key, agencies) in inputData.ClassifiedAgencies) {
				if (agencies == null || agencies.Count == 0) {
					continue;
				}
				List<DataReporter> classifiedReporters = new List<DataReporter>(agencies);
				result.Add(key, classifiedReporters);
			}
			return result;
		}

		public Dictionary<string, List<DataReporter>> CreateReportersByNetwork(Network network) {
			Dictionary<string, List<DataReporter>>
				result = new Dictionary<string, List<DataReporter>>();
			var generatorFunc = this.DataReporterGenerateAction;
			if (generatorFunc == null) {
				return new Dictionary<string, List<DataReporter>>();
			}
			foreach (var @delegate in generatorFunc.GetInvocationList()) {
				var func = (DataReporterGenerateDelegate) @delegate;
				result.AddRange(func(network));
			}
			return result;
		}

	}
}